package com.bootdo.collection.controller;

import com.bootdo.collection.domain.SourceStationDO;
import com.bootdo.collection.service.SourceStationService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 采集站点栏目的控制层
 */
@Controller
@RequestMapping("/collection/sourceStation")
public class SourceStaionController {
    String prefix = "collection/station/source";
    @Autowired
    private SourceStationService sourceStationService;

    /**
     * 跳转到采集栏目页面
     * @return
     */
    @GetMapping()
    public String SourceStation(){
        return prefix + "/source_station";
    }

    @Log("编辑采集源站点")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, Model model){
        SourceStationDO stationDO = sourceStationService.get(id);
        model.addAttribute("stationDO", stationDO);
        return prefix+"/edit";
    }

    @Log("更新采集源名称")
    @PostMapping("/update")
    @ResponseBody
    public R update(@Validated SourceStationDO sourceStationDO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            for(FieldError fieldError:bindingResult.getFieldErrors()){
                return R.error(fieldError.getField());
            }
        }
        if(sourceStationService.update(sourceStationDO) > 0){
            return R.ok();
        }
      return R.error();
    }



   @Log("添加站点或栏目")
   @GetMapping("/add/{id}")
   public String add(Model model,@PathVariable("id")int id){
     model.addAttribute("type",id);
     return prefix + "/add";
   }



   @Log("保存")
   @PostMapping("/save")
   @ResponseBody
    public R save(@Validated SourceStationDO sourceStationDO, BindingResult bindingResult){
       if(bindingResult.hasErrors()){
           for(FieldError fieldError:bindingResult.getFieldErrors()){
               return R.error(fieldError.getDefaultMessage());
           }
       }
        if(sourceStationService.save(sourceStationDO) > 0){
            return R.ok();
        }
        return R.error();
   }

    /**
     *查询采集源的栏目信息,由bootstrap框架渲染成层级结构
     * @return tree对象的json格式数据
     */
    @GetMapping("/tree")
    @ResponseBody
    public Tree<SourceStationDO> tree() {
        Tree<SourceStationDO> tree = sourceStationService.getTree();
        return tree;
    }


    /**
     *根据分页信息查询栏目信息
     * @param params 条件： offset:查询的起始位置   limit:查询的条件
     * @return
     */
    @GetMapping("/getPageList")
    @ResponseBody()
    public PageUtils getPageList(@RequestParam Map<String, Object> params){
        List<SourceStationDO> list = sourceStationService.list(params);
        int total = sourceStationService.getTotal();
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }
}
